package main

import (
	"gitee.com/ameise84/e3dcs/internal/log"
	"gitee.com/ameise84/e3net"
	"gitee.com/ameise84/e3utils/sys"
)

type connCB struct{}

func (ts *connCB) OnAgentConnect(agent e3net.TcpAgent, ctx e3net.ConnContext, conn e3net.Conn) {
	log.GetLogger().Trace("OnAgentConnect").Println()
}

func (ts *connCB) OnAgentConnectFailed(agent e3net.TcpAgent, tag e3net.Tag, ctx e3net.ConnContext, err error) {
	log.GetLogger().Trace("OnAgentConnectFailed").Println()
}

func (ts *connCB) OnAgentClosedConn(agent e3net.TcpAgent, tag e3net.Tag, ctx e3net.ConnContext) {
	log.GetLogger().Trace("OnAgentClosedConn").Println()
}

type connCtx struct{}

func (ts *connCtx) OnRecv(conn e3net.Conn, bs []byte) ([]byte, error) {
	log.GetLogger().Trace("OnRecv").Str("recv msg", string(bs)).Println()
	return nil, nil
}

func main() {
	//创建连接
	s, _ := e3net.NewTcpAgent(&connCB{}, e3net.DefaultTcpAgentOptions())
	s.Start()
	ctx := &connCtx{}
	c, err := s.ConnectSync("t", ctx, "127.0.0.1:4554")
	if err != nil {
		panic(err)
	}
	//发送消息
	c.SendSync([]byte("hello world"))
	log.GetLogger().Info("connect success").Println()
	sys.WaitKillSigint()
}
